package in.muton.panels.paginator.badgehelpers.user;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.components.messaging.api.UserMessage;
import in.muton.edushare.processing.objects.CourseDataHandler;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.EnrolmentsDAO;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Users;

public class CourseBadgeRoles extends BadgeRoles {
	private static final long serialVersionUID = 5803146349856530998L;
	public CourseBadgeRoles(IModel<CourseInstances> inst) {
		this.courseInst = inst;
		roleMap = new HashMap<String, Integer>();
		roleMap.put("Faculty", Enrolments.TEACHER);
		roleMap.put("Teaching Assistant", Enrolments.TEACHING_ASSISTANT);
		roleMap.put("Student", Enrolments.STUDENT);
		roleMap.put("Audit Student", Enrolments.OBSERVER);
	}

	@Override
	public String changeRole(Users user, String role) {
		if(isLastAdmin(user))
			return "Cannot change role for user "+user.getCompleteUserName() 
			+" as this user is the last faculty for this course.";
		
		
		String message = "The role of " + user.getCompleteUserName()
		+ " in the course "
		+ courseInst.getObject().getCourse().getCourseName()
		+ " has been changed to " + role + (getRole(user).equals("")?"":" from "+getRole(user));
		
		
		EnrolmentsDAO eDAO = new EnrolmentsDAO(EduShareSession.get()
				.getSessionORMInterface());
		eDAO.changeRole(courseInst.getObject(), user, roleMap.get(role));
		
		CayenneList<Users> userList = new CayenneList<Users>(null);
		userList.add(user);
		CourseDataHandler cdh = new CourseDataHandler(courseInst); 
		userList.add(cdh.getTeachers());
		userList.add(EduShareSession.get().getLoggedInUser());
		userList.add(new DeptDataHandler(cdh.getOwner()).getPrimaryAdmins());
		new UserMessage(message).setReciever(userList).setReciever(
				courseInst.getObject()).setPrivacy(Messages.PRIVACY_TYPE_PUBLIC)
				.send();
		return "";
	}

	@Override
	public List<String> getOtherRoles(Users user) {
		List<String> rList = new ArrayList<String>();
		int role = CourseRoleCheck.getRole(courseInst.getObject(), user);
		for(String key:roleMap.keySet()){
			if(roleMap.get(key)!=role)
				rList.add(key);
		}
		return rList;
	}

	@Override
	public String removeUser(Users user) {
		CourseRoleCheck crc = new CourseRoleCheck(courseInst.getObject(), user);
		if(new CourseDataHandler(courseInst).getTeachers().getObject().size() == 1 &&
				crc.isTeacher()){
			return "Cannot remove "+ user.getCompleteUserName()+" as this user is the only teacher for this course";
		}
		
		
		EnrolmentsDAO eDAO = new EnrolmentsDAO(EduShareSession.get()
				.getSessionORMInterface());
		eDAO.removeUser(courseInst.getObject(), user);
		
		String message = "The user "+user.getCompleteUserName()+" has been removed from"+
			" the course "+ courseInst.getObject().getCourse().getCourseName();
		
		CayenneList<Users> userList = new CayenneList<Users>(null);
		userList.add(user);
		CourseDataHandler cdh = new CourseDataHandler(courseInst); 
		userList.add(cdh.getTeachers());
		userList.add(EduShareSession.get().getLoggedInUser());
		userList.add(new DeptDataHandler(cdh.getOwner()).getPrimaryAdmins());
		new UserMessage(message).setReciever(userList).setReciever(
				courseInst.getObject()).setPrivacy(Messages.PRIVACY_TYPE_PUBLIC)
				.send();
		return "";
	}
	
	@Override
	public String getRole(Users user) {
		Integer role = CourseRoleCheck.getRole(courseInst.getObject(), user);
		Iterator<String> it = roleMap.keySet().iterator();
		while(it.hasNext()){
			String s = it.next();
			if(roleMap.get(s).equals(role)){
				return s;
			}
		}
		return "";
	}

	@Override
	public boolean isAdmin() {
		Users user = EduShareSession.get().getLoggedInUser();
		if(CourseRoleCheck.isAdmin(courseInst.getObject(), user))
			return true;
		if (CourseRoleCheck.isTeacher(courseInst.getObject(), user)
				&& !CourseRoleCheck.isTA(courseInst.getObject(), user))
			return true;
		return false;
	}
	
	
	private HashMap<String, Integer> roleMap = null;
	private IModel<CourseInstances> courseInst = null;
	
	@Override
	public boolean isInvited(Users user) {
		Enrolments matchingEnrolment = null;
		for(Enrolments e: user.getEnrolments()){
			if(e.getCourseInstance().equals(courseInst.getObject())){
				matchingEnrolment = e;
				break;
			}
		}
		
		if(matchingEnrolment.getStatus() == Enrolments.INVITED){
			return true;
		}	
		return false;
	}

	@Override
	public boolean isLastAdmin(Users user) {
		if(
				roleMap.get(getRole(user))!=null &&
				roleMap.get(getRole(user)).equals(Enrolments.TEACHER) && 
				new CourseDataHandler(courseInst).getTeachers().getObject().size()<2
		)
		{
			return true;
		}
		return false;
	}

}
